package com.gexin.model.agent;

import com.gexin.common.utils.SQLUtils;
import com.gexin.common.utils.StringUtil;
import com.gexin.model.agent.base.BaseAgent;
import com.jfinal.plugin.activerecord.Page;
import java.io.PrintStream;
import java.util.List;

public class Agent
		extends BaseAgent<Agent>
{
	public static final Agent dao = new Agent();
	public static final Integer STATUS_SUCCESS = Integer.valueOf(1);
	public static final Integer STATUS_WEICHULI = Integer.valueOf(0);
	public static final Integer STATUS_FAil = Integer.valueOf(2);
	public static final Integer TYPE_NORM = Integer.valueOf(1);
	public static final Integer TYPE_AREA = Integer.valueOf(0);
	public static final Integer TYPE_ER = Integer.valueOf(2);
	public static final Integer TYPE_TEYUE = Integer.valueOf(3);
	public static final Integer IS_ERJI_TRUE = Integer.valueOf(1);
	public static final Integer IS_ERJI_FALSE = Integer.valueOf(0);
	public static final Integer ISPALTFROMTRUE = Integer.valueOf(1);
	public static final Integer ISPALTFROMFALSE = Integer.valueOf(0);
	public static final Integer PAY_TYPE_WEIZHIFU = Integer.valueOf(0);
	public static final Integer PAY_TYPE_YIZHIFU = Integer.valueOf(1);
	public static final Integer CASH_DEPOSIT_WEIZHIFU = Integer.valueOf(0);
	public static final Integer CASH_DEPOSIT_YIZHIFU = Integer.valueOf(1);
	public static final Integer SEND_MAIL_TRUE = Integer.valueOf(1);

	public Page<Agent> findAgentList(int page, int pageSize, String name, String phone, String type)
	{
		SQLUtils sql = new SQLUtils();
		sql.append(" from t_agent agent ");
		sql.append(" left join t_agent_level level on level.id=agent.level_id ");
		sql.append(" where agent.status=1 ");
		if (!StringUtil.isBlank(name)) {
			sql.whereLike("agent.agent_name", name);
		}
		if (!StringUtil.isBlank(phone)) {
			sql.whereLike("agent.agent_phone", phone);
		}
		if (!StringUtil.isBlank(type)) {
			sql.whereEquals("agent.type", type);
		}
		sql.append(" order by modified_time desc ");

		System.out.println("select agent.*,level.discount as discountNumber " + sql.toString());
		return paginate(page, pageSize, "select agent.*,level.discount as discountNumber ", sql.toString());
	}

	public Agent findInfoByUserIdAndStatus(Integer userId, Integer status)
	{
		return (Agent)findFirst("select * from t_agent where user_id=? and status=?", new Object[] { userId, status });
	}

	public Agent findInfoByUserId(Integer userId)
	{
		return (Agent)findFirst("select * from t_agent where user_id=? and status=1", new Object[] { userId });
	}

	public Agent findInfoByUserIdALL(Integer userId)
	{
		return (Agent)findFirst("select * from t_agent where user_id=?", new Object[] { userId });
	}

	public Page<Agent> findAuthorizedDealer(Integer page, Integer pageSize, Integer agentId)
	{
		SQLUtils sql = new SQLUtils();
		sql.append(" from t_agent where type=3 and parent_id=? ");
		return paginate(page.intValue(), pageSize.intValue(), "select * ", sql.toString(), new Object[] { agentId });
	}

	public Page<Agent> findByExtensionAgentId(int page, int pageSize, String agentId)
	{
		SQLUtils sql = new SQLUtils(" from t_agent  agent ");
		sql.append(" where agent.parent_id=?  ");
		return paginate(page, pageSize, " select agent.*  ", sql.toString(), new Object[] { agentId });
	}

	public List<Agent> downLoadAgent(String times)
	{
		SQLUtils sql = new SQLUtils();
		sql.append(" from t_agent agent ");
		sql.append(" left join t_agent_level level on level.id=agent.level_id ");
		sql.append(" where agent.status=1 ");
		if (!StringUtil.isBlank(times))
		{
			String[] split = times.split(" - ");
			if (split.length == 2) {
				sql.between("agent.modified_time", split[0], split[1]);
			}
		}
		return find("select agent.*,level.discount as discountNumber " + sql.toString());
	}

	public Agent findInfoByPhoneAndStatus(String phone, Integer status)
	{
		return (Agent)findFirst("select * from t_agent where agent_phone=? and status=?", new Object[] { phone, status });
	}
}
